package com.lazy.utils.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;

/**
 * @package: com.credithc.hccloud.common.config.datasource
 * @description:
 * @author: 刘晨旭
 * @author_email: liuchenxu20170913@credithc.com
 * @create_time: 2018/8/29 15:46
 */
@Data
@ToString
@Slf4j
public class DruidConfig {
    private String url;
    private String username;
    private String password;
    private int initialSize;
    private int minIdle;
    private int maxActive;
    private String validationQuery;
    private int minEvictableIdleTimeMillis;
    private int timeBetweenEvictionRunsMillis;
    private Boolean testWhileIdle;
    private Boolean testOnBorrow;
    private Boolean testOnReturn;
    private Boolean poolPreparedStatements;
    private int maxPoolPreparedStatementPerConnectionSize;
    private String filters;

    static void build(DruidDataSource druidDataSource, DruidConfig druidConfig) {
        druidDataSource.setUrl(druidConfig.getUrl());
        druidDataSource.setUsername(druidConfig.getUsername());
        druidDataSource.setPassword(druidConfig.getPassword());
        druidDataSource.setInitialSize(druidConfig.getInitialSize());
        druidDataSource.setMinIdle(druidConfig.getMinIdle());
        druidDataSource.setMaxActive(druidConfig.getMaxActive());
        druidDataSource.setValidationQuery(druidConfig.getValidationQuery());
        druidDataSource.setMinEvictableIdleTimeMillis(druidConfig.getMinEvictableIdleTimeMillis());
        druidDataSource.setTimeBetweenConnectErrorMillis(druidConfig.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setTestWhileIdle(druidConfig.getTestWhileIdle());
        druidDataSource.setTestOnBorrow(druidConfig.getTestOnBorrow());
        druidDataSource.setTestOnReturn(druidConfig.getTestOnReturn());
        druidDataSource.setPoolPreparedStatements(druidConfig.getPoolPreparedStatements());
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidConfig.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            druidDataSource.setFilters(druidConfig.getFilters());
        } catch (SQLException e) {
            log.error("druid configuration initialization filter", e);
        }
    }
}
